Skip to content

fix(ai): crash-proof SSE parsing + drop dead AiResult.RateLimited#8

Closed
Azyrn wants to merge 2 commits into
mainfrom
fix/core-ai-audit
Closed

fix(ai): crash-proof SSE parsing + drop dead AiResult.RateLimited#8
Azyrn wants to merge 2 commits into
mainfrom
fix/core-ai-audit

Conversation

@Azyrn

@Azyrn Azyrn commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Two fixes from the core/ai audit, worst-severity first. Scoped to findings #1#2 only; #3–#10 deferred.

#1 — Malformed SSE chunk crashes the scan pipeline (critical)

ProviderClient.parseStreamPiece decoded every SSE line with decodeFromString; a schema/type mismatch (OpenAI-compatible providers send error code as a string like "rate_limit_exceeded", or a truncated mid-stream chunk) threw a SerializationException that ProviderExecutor.run does not catch — it escaped the retry/fallback loop and failed the whole flow with a raw exception.

  • ApiError.code Int?JsonElement?, read via new codeInt helper (numeric only).
  • New decodeChunk<T>() wraps the streaming decode for all 3 kinds; parse failure → TransientAiException.
  • Two OpenAI err.code == 429err.codeInt == 429.

ProviderExecutor already treats TransientAiException as retryable, so a bad chunk now degrades to a normal retry / provider fallback instead of a crash.

#2 — Remove never-produced AiResult.RateLimited (moderate)

The variant was never constructed anywhere; rate limits already surface as AiResult.Error strings. Three UI branches handling it were dead and remainingMs had no source.

  • Deleted the variant + dead branches in ResultsScreen and AiScanViewModel (extraction + translation).
  • Rate-limited translation now falls through to the existing Error branch → shows "Translation failed" instead of failing silently (intended, kept per review).

Testing

  • :app:compileDebugKotlin clean after each commit.
  • grep RateLimited across app/src/main/java → none remain; when expressions stay exhaustive over Success/Error.
  • Traced retry path: decodeChunk throw → consumeSse body.use{} closes body → stream()withTimeoutProviderExecutor catch (TransientAiException) → backoff retry → Exhausted → next provider in chain.

Not in scope

Findings #3–#10 (duplicate endpoint/auth knowledge, repeated 429 parse blocks, Anthropic once() 429 gap, presentation copy in core, dead ICON_TRANSLATE / OcrEngine.displayName, HF verify false-positive, text-mime error message).

🤖 Generated with Claude Code

Azyrn and others added 2 commits July 6, 2026 00:35
OpenAI-compatible providers may send the error `code` as a string
("rate_limit_exceeded") instead of an int, and a mid-stream chunk can
be schema-mismatched or truncated. Either case threw a
SerializationException from decodeFromString in parseStreamPiece that
ProviderExecutor.run does not catch, so it escaped the retry/fallback
loop and failed the whole flow with a raw exception.

Loosen ApiError.code to JsonElement and read it via a codeInt helper,
and wrap the streaming decode in decodeChunk so a parse failure is
re-thrown as TransientAiException. ProviderExecutor already treats
TransientAiException as retryable, so a bad chunk now degrades to a
normal retry / provider fallback instead of a crash.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
AiResult.RateLimited was never constructed anywhere in the pipeline —
rate limits already surface as AiResult.Error strings via
exhaustedMessage / httpErrorMessage. The three UI branches handling it
(ResultsScreen, AiScanViewModel extraction + translation) were dead,
and its remainingMs had no source. Remove the variant and its dead
consumers; the sealed when-expressions stay exhaustive over
Success/Error, and a rate-limited translation now falls through to the
existing Error branch.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
scanly Ready Ready Preview, Comment Jul 5, 2026 11:41pm

@Azyrn Azyrn closed this Jul 6, 2026
@Azyrn Azyrn deleted the fix/core-ai-audit branch July 6, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant